﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Action
{
    public class IndexActionModel
    {
        public List<IndexActionInfo> Actions
        {
            get
            {
                return actionList;
            }
        }

        private List<IndexActionInfo> actionList;
        riskanalysisEntities entities = new riskanalysisEntities();

        public IndexActionModel(int dangerId)
        {
            SearchActionsByDangerId(dangerId);
        }

        public void SearchActionsByDangerId(int dangerId)
        {               
            var query = from a in entities.actions
                        where a.dangerId == dangerId
                        orderby a.actionId
                        select a;
            actionList = new List<IndexActionInfo>();
            List<action> list = new List<action>();
            

            foreach (action item in query)
            {
                list.Add(item);
            }

            foreach (var item in list)
            {
                string executionDate = item.executionDate.ToString("d", Utils.culture);

                executionDate = executionDate.Equals("01/01/2000") ? "Sin fecha" : executionDate;
                actionList.Add(
                    new IndexActionInfo
                    {
                        ActionId = item.actionId,
                        ActionType = item.actiontype.name,
                        Description = item.description,
                        ExecutionDate = executionDate,
                        ProgrammedDate = item.programedDate.ToString("d", Utils.culture),
                        Responsible = item.user.firstName + " " + item.user.lastName + " " + item.user.secondLastName,
                        Status = item.status.ToString() + "%"
                    });
            }
        }
    }
}